const tags = ['api', 'shops']
const Joi = require('joi')
const {
  shops
} = require("../../models")
const {
  paginationDefine
} = require('../../utils/router-helper');

module.exports = [{
    method: 'GET',
    path: '/shops',
    config: {
      tags: tags,
      description: '店铺列表',
      validate: {
        query: {
          ...paginationDefine
        }
      }
    },
    handler: async (request, h) => {
      const {
        rows: data,
        count: totalCount
      } = await shops.findAndCountAll({
        attributes: [
          'id',
          'name',
        ],
        limit: request.query.limit,
        offset: (request.query.page - 1) * request.query.limit,
      })
      console.log(totalCount);
      return {
        results: data,
        totalCount,
        status: 1
      }
    }
  }, {
    method: 'GET',
    path: '/shops/goods',
    config: {
      tags: tags,
      description: '获取店铺的商品列表'
    },
    handler: (request, h) => {

      return 'Hello, world!';
    }
  },
  {
    method: 'post',
    path: '/shops/addGood',
    config: {
      tags: tags,
      description: '添加店铺',
      auth: false,
      validate: {
        payload: {
          name: Joi.string().required().description('名称'),
          status: Joi.string().required().description('昵称'),
        }
        // payload: {
        //   code: Joi.string().required().description('微信用户登录的临时code'),
        //   encryptedData: Joi.string().required().description('微信用户信息encryptedData'),
        //   iv: Joi.string().required().description('微信用户信息iv'),
        // }
      }
    },
    handler: async (request, h) => {
      console.log(request.payload);
      let {
        name,
        status
      } = request.payload
      console.log(name);
      const a = await shops.create({
        name
        // status
      })
      return {
        success: 1
      }
      // return {
      //   a:'123'
      // }
    }
  }
]
